' iBcMatches.ibas
{CREATORID "LDIT"}
{VERSION "1.2"}

_NbLeft:
   PRINT "There ";
   IF N=1 THEN PRINT "is ";
   ELSE PRINT "are ";
   ENDIF
   PRINT N USING 0;
   PRINT " match";
   IF N>1 PRINT "es";
   PRINT " left"
RETURN

_NbRemoved:
   PRINT " remove ";
   PRINT R USING 0;
   PRINT " match";
   IF R>1 PRINT "es";
   PRINT "..."
   N=N-R
   GOSUB _NbLeft
RETURN

_ComputerPlays:
   M=N-1 MOD 4
   IF M=0 THEN
      REPEAT
         R=RND(3)+1
      UNTIL R<N
   ELSE R=M
   ENDIF
   PRINT "I";
   GOSUB _NbRemoved
RETURN

_HumanPlays:
   PRINT "Remove how many matches ?"
   REPEAT
      Q=1
      INPUT R
      IF R<1 LET Q=0
      IF R>3 LET Q=0
      IF R>=N LET Q=0
   UNTIL Q=1
   PRINT "You";
   GOSUB _NbRemoved	
RETURN

_InitGame:
   N=RND(29)+2
   CLS
   A$=SPACE$(10) : PRINT A$;
   PRINT "Matches"
   PRINT
   GOSUB _NbLeft
   PRINT "Please choose who will start"
   PRINT "first to remove matches:"
   PRINT "[1] Me (Computer)"
   PRINT "[2] You (Human)"
   Q=0
   REPEAT
      INPUT F
      IF F=1 LET Q=1
      IF F=2 LET Q=1
   UNTIL Q=1
   IF F=1 GOSUB _ComputerPlays
RETURN

_PlayAgain:
   PRINT "Do you wish to play again ?"
   PRINT "[1] Yes"
   PRINT "[2] No"
   Q=0
   REPEAT
      INPUT G
      IF G=1 LET Q=1
      IF G=2 LET Q=1
   UNTIL Q=1
   IF G=1 THEN GOSUB _InitGame
   ELSE N=0
   ENDIF
RETURN

_ComputerWins:
   PRINT
   PRINT "Sorry, I win !"
   PRINT
   GOSUB _PlayAgain
RETURN

_HumanWins:
   PRINT
   PRINT "Good job, you win."
   PRINT
   GOSUB _PlayAgain
RETURN

_MainLoop:
   REPEAT
      IF N=1 THEN
         GOSUB _ComputerWins
      ELSE
         GOSUB _HumanPlays
         IF N>0 THEN
            IF N=1 THEN
               GOSUB _HumanWins
            ELSE
               GOSUB _ComputerPlays
            ENDIF
         ENDIF
      ENDIF
   UNTIL N=0
RETURN

BEGIN
GOSUB _InitGame
GOSUB _MainLoop
END